﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseAccessLayer;
using System.Configuration;

namespace WebApplication.administrator
{
    public partial class DeletedUsers : System.Web.UI.Page
    {
        Int16 PageSize = Convert.ToInt16(ConfigurationManager.AppSettings["PageSize"]);
        int PageNumber = 1;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["Admin"] == null)
            {
                Response.Redirect("Sign-In.aspx");
            }
            if (!IsPostBack)
            {
                FillUserList(PageNumber, PageSize);
                ltrPageNumber.Text = PageNumber.ToString();

            }
            if (PageNumber <= 1)
            {
                lnkPrevious.Enabled = false;
            }
            else
            {
                lnkPrevious.Enabled = true;
            }
            if (PageNumber >= Convert.ToInt32(ltrTotalPage.Text))
            {
                lnkNext.Enabled = false;
            }
            else
            {
                lnkNext.Enabled = true;
            }


        }
        public void FillUserList(int PageNumber, int PageSize)
        {
            List<AdminDisplayUserList> ListUsers = AdminUser.GetListOfAllUsers();
                
                ltrTotalPage.Text = PageCount(ListUsers.Count).ToString();
                var user = (from list in ListUsers
                            where list.IsDeleted==true
                            select new { list.UserID, list.Name, list.AccountType, list.Country, list.AccountNo }).Skip((PageNumber - 1) * PageSize).Take(PageSize);
                if (null != user)
                {
                    
                    gvUserList.DataSource = user;
                    gvUserList.DataBind();
                }
                else
                {
                    divPage.Visible = false;
                    gvUserList.Columns.Clear();
                    gvUserList.EmptyDataText = "Result is not found";
                }
            
        }
        public void btnDeleteUser(object sender, EventArgs e)
        {
            int UserID = Convert.ToInt32(((LinkButton)sender).CommandArgument);
            if (UserID > 0)
            {
                AdminUser.DeleteUser(UserID.ToString());
                FillUserList(PageNumber, PageSize);
            }
        }
        public void btn_DeleteSelected(object sender, EventArgs e)
        {
            string UserIDs = string.Empty;
            foreach (GridViewRow grdrow in gvUserList.Rows)
            {
                CheckBox chkBx = (CheckBox)grdrow.FindControl("chkSelect");

                if (chkBx != null && chkBx.Checked)
                {
                    long UserID = (long)gvUserList.DataKeys[grdrow.DataItemIndex].Value;
                    UserIDs = UserID + ",";
                }


            }
            if (UserIDs != string.Empty)
            {
                AdminUser.DeleteUser(UserIDs.Substring(0, UserIDs.Length - 1));
                FillUserList(PageNumber, PageSize);
            }

        }

        private int PageCount(int records)
        {
            int intPart = Convert.ToInt32(records / PageSize);
            if (Math.Ceiling((decimal)records / PageSize) != 0)
            {
                intPart = intPart + 1;
            }
            return intPart;
        }
        public void lnkPrevious_Click(object sender, EventArgs e)
        {
            PageNumber = Convert.ToInt32(ltrPageNumber.Text) + 1;
            FillUserList(PageNumber, PageSize);
            ltrPageNumber.Text = PageNumber.ToString();
        }
        public void lnkNext_Click(object sender, EventArgs e)
        {
            PageNumber = PageNumber - 1;
            FillUserList(PageNumber, PageSize);
            ltrPageNumber.Text = PageNumber.ToString();
        }
    }
}